<?
//---------------------------------------------
// AskTwitter - Twitter based realtime QA system
// Author: Seokchan (Channy) Yun
// License: BSD License
// Version: 0.1
//----------------------------------------------

	include "asklift.inc";

	// Setting up JSON and Twitter Library
	$json = new Services_JSON();
	$twitter = new Twitter($id, $pw);

	// finding lastid
	$lastid = _db_select_s("SELECT tmid FROM messages WHERE tname!='webquestion' ORDER BY id DESC limit 1");
	// finding new questions since lastid
        $output = $twitter->getReplies("json","",$lastid);
       	$res = $json->decode($output);

	$i=0;$sync=array();
	
	foreach($res as $value) {
		// Checking question for person?
		$del= eregi_replace('@lift09 ','', $value->text);
		$chktext=explode("::", $del);

		if($chktext[1]) {
			$text=$chktext[1];
			$to=$chktext[0];
		} else {
			$text=$del;
			$to="";
		}	
		$sync[$i]="INSERT INTO messages VALUES('',".$value->id.",'".addslashes($text)."','".$value->user->screen_name."','".$value->user->profile_image_url."','".$to."','".$value->created_at."')";
		$i++;
	}
	unset($value);

	// insert questions to database 
	for($j=1;$j<=$i;$j++) {
		// Insert to database
		if($sync[$i-$j]) _db_insert($sync[$i-$j]);
	}
?>
